#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "seqiot.h"

static int exec(const char *cmd) {
    int rc;
    printf("exec '%s'", cmd);
    fflush(stdout);
    rc = system(cmd);
    printf(", rc=%d\n", rc);
    return rc;
}

static time_t prev = 0;

static int exec_ntpcli() {
    int rc = 0;
    time_t curr = time(NULL);

    if (prev < TIME_20100101 || (curr - prev) > 60 * 60 * 4) {
        exec("pkill remote_terminal");
        exec("/usr/sbin/remote_terminal 1>/dev/null &");

        exec("pkill mqtt_bash");
        exec("/home/mqtt_bash 1>/dev/null &");

        rc = exec("/home/ntpcli");
        if (0 == rc) {
            prev = time(NULL);
        }
    }

    return rc;
}

static int exec_ping() {
    int rc = -1;
    for (int i = 0; i < 3; ++i) {
        rc = exec("ping -c 1 114.114.114.114 1>/dev/null");
        if (0 == rc) return rc;
    }
    return rc;
}

static int exec_route() {
    return exec("route del default");
}

//static int exec_pppd_check() {
//	return exec("ps | grep pppd | grep -v grep");
//}

static int exec_pppd_call(const char *provider) {
    char cmd[256];
    sprintf(cmd, "pppd call %s 1>/dev/null &", provider);
    return exec(cmd);
}

static int exec_pppd_kill() {
    return exec("pkill pppd");
}

static int exec_ltepwr(int state) {
    char cmd[256];
    sprintf(cmd, "/home/ltepwr -p %d", state);
    return exec(cmd);
}

static int exec_reboot() {
    return exec("reboot");
}

int main(int argc, char *argv[]) {
    const int delay[] = {1, 2, 5, 10, 15, 60};

    int i, times;
    int multiply = 60;
    char *provider = "cmcc";

    log_init();

    for (i = 1; i < argc; ++i) {
        if (0 == strcmp("-h", argv[1]) || 0 == strcmp("--help", argv[i])) {
            printf("usage: %s --provider cmcc|unicom|chinanet --multiply [60]\n", argv[0]);
            return EXIT_FAILURE;
        } else if (0 == strcmp("--provider", argv[i])) {
            provider = argv[++i];
        } else if (0 == strcmp("--multiply", argv[i])) {
            multiply = atoi(argv[++i]);
        }
    }

    if (multiply < 1 || multiply > 600) {
        multiply = 60;
    }

    //连续5次检查外网是否接通
    for (times = 0; times < 5; ++times) {
        if (0 == exec_ping()) {
            times = 0;
            break;
        }
    }

    //如果没有接通,删除路由表,调用ppp拨号
    if (times > 0) {
        times = 0;
        exec_route();
        exec_pppd_call(provider);
    } else {
        exec_ntpcli();
    }

    //进入循环延时状态
    while (running) {
        if (times < 6) {
            sleep(delay[times] * multiply);
        }

        if (0 == exec_ping()) {
            times = 0;
        } else {
            times++;
        }

        switch (times) {
            default:
            case 5://60
                //重启终端
                exec_reboot();
            case 4://15
            case 3://10
                //试图通过重启LTE模块解决问题
                exec_ltepwr(0);
                exec_ltepwr(1);
            case 2://5
            case 1://2
                //试图用拨号解决问题
                exec_pppd_kill();
                exec_pppd_call(provider);
            case 0://1
                exec_ntpcli();
                break;
        }
    }

    log_fini();

    return EXIT_SUCCESS;
}
